#include <cstdio>
#include <algorithm>
#include <cstring>

using namespace std;

const int maxn=500000, inf=0x7ffffff;
int n, m, ind[maxn+1], v[maxn+1], to[2*maxn+1], link[2*maxn+1], f[maxn+1][3][2], cnt[maxn+1];

void addEdge(int u, int v) {
    m++;
    to[m] = v;
    link[m] = ind[u];
    ind[u] = m;
}

void solve(int o, int fa) {
    memset(f[o], 50, sizeof f[o]);
    static int g[3][2];
    f[o][0][!v[o]]=1;
    cnt[o] = v[o];
    for (int i=ind[o]; i; i=link[i]) {
        int x=to[i];
        if (x!=fa) {
            solve(x, o);
            if (cnt[x]) {
                cnt[o] += cnt[x];
                memcpy(g, f[o], sizeof g);
                for (int u=0; u<2; u++) {
                    f[o][0][u] = min(g[0][!u]+f[x][0][0]+1, g[0][u]+f[x][0][1]+3);
                    f[o][1][u] = min(min(g[1][!u]+f[x][0][0]+1, g[1][u]+f[x][0][1]+3),
                                     min(g[0][u]+f[x][1][0], g[0][!u]+f[x][1][1]+2));
                    f[o][2][u] = min(min(g[2][!u]+f[x][0][0]+1, g[2][u]+f[x][0][1]+3),
                                     min(min(g[1][u]+f[x][1][0], g[1][!u]+f[x][1][1]+2),
                                         min(g[0][u]+f[x][2][1]+1, g[0][!u]+f[x][2][0]+3)));
                }
            }
        }
    }
    for (int u=0; u<2; u++) {
        f[o][1][u] = min(f[o][1][u], f[o][0][u]);
        f[o][2][u] = min(f[o][2][u], f[o][1][u]);
    }
}

int main() {
    freopen("light.in", "r", stdin);
    freopen("light.out", "w", stdout);

    int n;
    scanf("%d", &n);
    for (int i=1; i<=n; i++) {
        scanf("%1d", v+i);
        v[i] = !v[i];
    }
    for (int i=1; i<n; i++) {
        int x, y;
        scanf("%d %d", &x, &y);
        addEdge(x, y), addEdge(y, x);
    }

    int ans=inf;
    solve(1, 0);
    for (int i=1; i<=n; i++) {
        if (cnt[1]-cnt[i]==0) {
            ans = min(ans, min(f[i][2][0], min(f[i][1][0], f[i][0][0])));
        }
    }
    printf("%d", ans);

    fclose(stdin);
    fclose(stdout);
    return 0;
}
